import { QueryClient, useMutation, useQuery } from "@tanstack/react-query";
import { ThemeService } from "@/shared/api/api.theme";
import { AxiosError } from "axios";
import { ITheme } from "@/shared/interfaces";

export function useGetThemesByDisciplineId(disciplineId: number) {
    const queryClient = new QueryClient

    const loadThemesByDisciplineId = useQuery<ITheme[], (AxiosError<{ message: string }>)>(
        {
            retry: 3,
            queryKey: ['themes-by-discipline', disciplineId],
            queryFn: () => ThemeService.getByDisciplineId(disciplineId),
        }, queryClient
    );
    const errorMessage = loadThemesByDisciplineId.error ? loadThemesByDisciplineId.error.response?.data.message : undefined;


    return {
        errorMessage,
        refetch:loadThemesByDisciplineId.refetch,
        isSuccess: loadThemesByDisciplineId.isSuccess,
        themes: loadThemesByDisciplineId.data
    };
}